package com.study.boot.activiti.config;

import org.activiti.engine.RepositoryService;
import org.activiti.engine.repository.Deployment;
import org.activiti.engine.repository.DeploymentBuilder;
import org.activiti.engine.repository.DeploymentQuery;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.ImportResource;

import javax.annotation.Resource;
import java.util.List;

/**
 * @author TangFD@HF 2018/5/9
 */
@Configuration
@ImportResource(locations = {"activiti.cfg.xml"})
public class BeanConfig {
    @Resource
    private RepositoryService repositoryService;

    @Bean
    public String initActiviti() {
        DeploymentQuery deploymentQuery = repositoryService.createDeploymentQuery();
        List<Deployment> list = deploymentQuery.list();
        for (Deployment deployment : list) {
            repositoryService.deleteDeployment(deployment.getId(), true);
        }

        DeploymentBuilder deployment = repositoryService.createDeployment();
        DeploymentBuilder deploymentBuilder = deployment.addClasspathResource("process/leave.bpmn20.xml");
        deployment.name("SpringBoot-Activiti");
        Deployment deploy = deploymentBuilder.deploy();
        System.out.println("==========" + deploy.getId() + ";" + deploy.getKey() + ";" + deploy.getName());
        return null;
    }

}
